<?php
/**
 * <https://y.st./>
 * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org./licenses/>.
**/

$xhtml = array(
	'<{title}>' => 'mycalc.py',
	'<{subtitle}>' => 'Written in <span title="Programming Fundamentals">CS 1101</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalised on 2017-03-01',
	'<{copyright year}>' => '2017',
	'takedown' => '2017-11-01',
	'<{body}>' => <<<END
<h2>Script:</h2>
<blockquote>
<pre><code># Every good program should begin with a license comment that declares
# the file as being covered by a free software license.
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program. If not, see &lt;https://www.gnu.org./licenses/&gt;.

# This makes Python2 behave like python3 in regards to division.
from __future__ import division

# This makes Python2 behave like Python3 in regards to user input.
if &quot;raw_input&quot; in vars(__builtins__):
	input = raw_input

# User input is in string form, not number form. We need to not only
# make sure the user input a number, but also convert the string
# representation of that number into a numeric representation.
def sanitize_and_parse(string):
	try:
# If the input can be treated as an integer, we&apos;ll do that.
		return int(string)
	except:
		try:
# If the input cannot be treated as an integer but can be treated as a
# float, we&apos;ll do that instead.
			return float(string)
		except:
# The assignment instructions say to disallow the use of zeros as
# operands. Normally, invalid input shouldn&apos;t be treated as a valid
# number, but we&apos;re rejecting zero anyway, so we might as well use it
# as the return for bad input as well. That way, our zero-checker will
# automatically filter out bad input.
			return 0

# Display the equation thus far
print(&quot;Equation: [?] [?] [?] == [?]&quot;)

# Python doesn&apos;t have a &quot;do while&quot; loop construct. Let&apos;s instead use a
# variable and a while loop.
operand0 = 0

while operand0 == 0:
# Prompt for the first operand
	operand0 = input(&apos;Please provide the first operand (a non-zero number, if you will), then hit &quot;enter&quot;.\nOperand 0:  &apos;)
# Clean up the first operand
	operand0 = sanitize_and_parse(operand0)
	if operand0 == 0:
		print(&quot;\t[ERROR]: Only non-zero numbers can be used as input.&quot;)

# Display the equation thus far
print(&quot;Equation: &quot; + str(operand0) + &quot; [?] [?] == [?]&quot;)

operator = None

while operator != &quot;+&quot; and operator != &quot;*&quot; and operator != &quot;-&quot; and operator != &quot;/&quot;:
	operator = input(&apos;Please enter an operator, then hit &quot;enter&quot;. Valid operators are &quot;+&quot; (addition), &quot;*&quot; (multiplication), &quot;-&quot; (subtraction), and &quot;/&quot; (division).\nOperator:  &apos;)
	if operator != &quot;+&quot; and operator != &quot;*&quot; and operator != &quot;-&quot; and operator != &quot;/&quot;:
		print(&apos;\t[ERROR]: &quot;+&quot;, &quot;*&quot;, &quot;-&quot;, and &quot;/&quot; are the only supported operators.&apos;)

# Display the equation thus far
print(&quot;Equation: &quot; + str(operand0) + &quot; &quot; + operator + &quot; [?] == [?]&quot;)

operand1 = 0

while operand1 == 0:
# Prompt for the second operand
	operand1 = input(&apos;Please provide the second operand (a non-zero number, if you will), then hit &quot;enter&quot;.\nOperand 1:  &apos;)
# Clean up the second operand
	operand1 = sanitize_and_parse(operand1)
	if operand1 == 0:
		print(&quot;\t[ERROR]: Only non-zero numbers can be used as input.&quot;)

# Let&apos;s do some maths!
if operator == &quot;+&quot;:
	result = operand0 + operand1
elif operator == &quot;-&quot;:
	result = operand0 - operand1
elif operator == &quot;*&quot;:
	result = operand0 * operand1
else: # operator == &quot;/&quot;
	result = operand0 / operand1

# Display the completed equation, including the result
print(&quot;Equation: &quot; + str(operand0) + &quot; &quot; + operator + &quot; &quot; + str(operand1) + &quot; == &quot; + str(result))
</code></pre>
</blockquote>
<h2>Output:</h2>
<blockquote>
<pre>Python 2.7.9 (default, Jun 29 2016, 13:08:31) 
[GCC 4.9.2] on linux2
Type &quot;copyright&quot;, &quot;credits&quot; or &quot;license()&quot; for more information.
&gt;&gt;&gt; ================================ RESTART ================================
&gt;&gt;&gt; 
Equation: [?] [?] [?] == [?]
Please provide the first operand (a non-zero number, if you will), then hit &quot;enter&quot;.
Operand 0:  0
	[ERROR]: Only non-zero numbers can be used as input.
Please provide the first operand (a non-zero number, if you will), then hit &quot;enter&quot;.
Operand 0:  Five
	[ERROR]: Only non-zero numbers can be used as input.
Please provide the first operand (a non-zero number, if you will), then hit &quot;enter&quot;.
Operand 0:  215
Equation: 215 [?] [?] == [?]
Please enter an operator, then hit &quot;enter&quot;. Valid operators are &quot;+&quot; (addition), &quot;*&quot; (multiplication), &quot;-&quot; (subtraction), and &quot;/&quot; (division).
Operator:  q
	[ERROR]: &quot;+&quot;, &quot;*&quot;, &quot;-&quot;, and &quot;/&quot; are the only supported operators.
Please enter an operator, then hit &quot;enter&quot;. Valid operators are &quot;+&quot; (addition), &quot;*&quot; (multiplication), &quot;-&quot; (subtraction), and &quot;/&quot; (division).
Operator:  /
Equation: 215 / [?] == [?]
Please provide the second operand (a non-zero number, if you will), then hit &quot;enter&quot;.
Operand 1:  0
	[ERROR]: Only non-zero numbers can be used as input.
Please provide the second operand (a non-zero number, if you will), then hit &quot;enter&quot;.
Operand 1:  2
Equation: 215 / 2 == 107.5
&gt;&gt;&gt;</pre>
</blockquote>
END
);
